﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KhachSan.Process.Public;
using DevExpress.XtraEditors;
using KhachSan.Process.Private.Bo.Class;
using System.Runtime.InteropServices;

namespace KhachSan.Process.Private.Bo
{
    public partial class UC_SoDoPhong : UserControl
    {

        private int KHUNG_TRAI_WIDTH = 212;
        private const int KHU_WIDTH = 100;
        private const int TANG_WIDTH = 100;
        private const int TANG_HEIGHT = 100;
        private const int KHU_HEIGHT = 100;
        private int PHONG_WIDHT = 100;
        private int PHONG_HEIGHT = 100;
        private int loop_x = 15;
        private const int KHOAN_CACH_PHONG_HEIGHT = 10;
        private const int KHOAN_CACH_PHONG_WIDTH = 10;

        private DataTable khu, tang, phong;
        private FlowLayoutPanel currenKhu;
        private F_ThongTinPhong thongTinPhong;
        private List<int> mau;
        public Phong _phong;
        public Phong _Phong{
            get { return _phong; }
            set { _phong = value; }
        }
        public struct IconInfo
        {
            public bool fIcon;
            public int xHotspot;
            public int yHotspot;
            public IntPtr hbmMask;
            public IntPtr hbmColor;
        }

        enum TrangThaiPhong : int { dangTrong = 1, daCoKhachDat = 2, coKhach = 3, dangDon = 4, dangBaoTri = 5, khongBan = 6, phongDo = 7 };

        public UC_SoDoPhong()
        {
            InitializeComponent();
            this.MouseWheel += new MouseEventHandler(pnPhong_MouseWheel);
        }
        private void bntLoadTang_Click(object sender)
        {
            HienTang(sender);
        }
        private void tang_Click(object sender)
        {
            LoadPhong(sender);
        }
        private void Phong_Leave(UC_Phong phong)
        {
            Point c = System.Windows.Forms.Cursor.Position;
            Point p = this.PointToClient(c);
            p.Y = p.Y - panelThongTinSinhNhat.Height;
            if (!IsSelected(phong,p))
                thongTinPhong.Hide();
        }

        private void Phong_Hover(Class.Phong phong)
        {
            try
            {
                if (thongTinPhong != null)
                {
                    this._phong = phong;
                    thongTinPhong.Show();
                    thongTinPhong.BringToFront();
                    Point c = System.Windows.Forms.Cursor.Position;
                    Point p = this.PointToClient(c);
                    thongTinPhong._LoaiPhong.Text = phong.GiaPhong1.HangPhong._LoaiPhong.LoaiPH;
                    thongTinPhong._KieuPhong.Text = phong.GiaPhong1.HangPhong._KieuPhong.DienGiai;
                    thongTinPhong._Gia1Nguoi.Text = Class.Tien.setTextToCurrency(phong.GiaPhong1.Gia1Khach);
                    thongTinPhong._Gia2Nguoi.Text = Class.Tien.setTextToCurrency(phong.GiaPhong1.Gia2Khach);
                    thongTinPhong._Gia3Nguoi.Text = Class.Tien.setTextToCurrency(phong.GiaPhong1.Gia3Khach);
                    thongTinPhong._Gia4Nguoi.Text = Class.Tien.setTextToCurrency(phong.GiaPhong1.Gia4Khach);
                    thongTinPhong._GiaThemNguoiLon.Text = Class.Tien.setTextToCurrency(phong.GiaPhong1.GiaThemNguoiLon);
                    thongTinPhong._GiaThemTreEm.Text = Class.Tien.setTextToCurrency(phong.GiaPhong1.GiaThemTreEm);
                    thongTinPhong._TrangThai.Text = phong.TrangThai1.TrangThaiPhong;
                    Point temp=new Point(p.X,p.Y);
                    if (p.Y + thongTinPhong.Height > panelPhong.Height)
                        temp.Y = temp.Y - thongTinPhong.Height;
                    if (p.X + thongTinPhong.Width > panelPhong.Width)
                        temp.X = temp.X - thongTinPhong.Width;
                    thongTinPhong.SetBounds(temp.X,temp.Y, thongTinPhong.Width, thongTinPhong.Height);
                }
            }
            catch { }
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                panelPhong.Controls.Clear();
                VeSoDo(tbLyle.Value);
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        private void LoadPhong(object sender)
        {
            try
            {
                string maTang = ((FlatButton)sender).Tag.ToString();
                phong = XLDuLieu.LoadDataTable("sp_LayThongTinSoDoPhong " + maTang);
                panelPhong.Controls.Clear();
                string trangThaiPhong;
                int magin = 6;
                if (phong != null)
                {
                    for (int i = 0; i < phong.Rows.Count; i++)
                    {
                        DataRow r = phong.Rows[i];
                        UC_Phong uc_Phong = new UC_Phong();
                        Class.Phong objPhong = new Class.Phong(r["ID_Tang"].ToString()
                                                    , r["TenTang"].ToString()
                                                    , r["MaTTP"].ToString()
                                                    , r["TrangThai"].ToString()
                                                    , (int)r["Mau"]
                                                    , r["MaPH"].ToString()
                                                    , r["MaGia"].ToString()
                                                    , r["DienGiaiGiaPhong"].ToString()
                                                    , r["TuNgay"].ToString()
                                                    , r["DenNgay"].ToString()
                                                    , (int)r["Gia1Khach"]
                                                    , (int)r["Gia2Khach"]
                                                    , (int)r["Gia3Khach"]
                                                    , (int)r["Gia4Khach"]
                                                    , (int)r["GiaThemNGLon"]
                                                    , (int)r["GiaThemTreEm"]
                                                    , r["MaLP"].ToString()
                                                    , r["LoaiPH"].ToString()
                                                    , r["MaKP"].ToString()
                                                    , r["DienGiaiKieuPhong"].ToString()
                                                    , r["MaHangPhong"].ToString()
                                               );
                        uc_Phong._Phong = objPhong;
                        uc_Phong._TieuDe.Text = objPhong.MaPH;
                        trangThaiPhong = objPhong.TrangThai1.MaTTP;
                        PHONG_WIDHT = PHONG_HEIGHT = (panelPhong.Width - magin * loop_x) / loop_x;
                        uc_Phong.di = new UC_Phong.leave(Phong_Leave);
                        uc_Phong.hover = new UC_Phong.Hover(Phong_Hover);
                        uc_Phong._TrangThaiPhong.BackColor = Color.FromArgb(objPhong.TrangThai1.Mau);
                        uc_Phong.Width = PHONG_WIDHT;
                        uc_Phong.Height = PHONG_HEIGHT;
                        uc_Phong.Phong_Move = new UC_Phong.DuyChuyen(Phong_Move);
                        if (trangThaiPhong == ((int)TrangThaiPhong.coKhach).ToString())
                        {
                            // uc_Phong._light.Image = Image.FromFile(Application.StartupPath + "/hinh_bo/yellow_light.png");
                        }
                        else
                        {

                        }
                        panelPhong.Controls.Add(uc_Phong);

                    }
                }
            }
            catch { }
        }
        bool IsSelected(UC_Phong phong,Point p)
        {
            if (p.X < phong.Left)
                return false;
            if (p.Y < phong.Top)
                return false;
            if (p.X > phong.Left + phong.Width)
                return false;
            if (p.Y > phong.Top + phong.Height)
                return false;
            return true;
        }
        private void Phong_Move(UC_Phong phong)
        {
            try
            {
                Point c = System.Windows.Forms.Cursor.Position;
                Point p = this.PointToClient(c);
                p.Y = p.Y - panelThongTinSinhNhat.Height;
                for (int i = 0; i < panelPhong.Controls.Count; i++)
                {
                    if (panelPhong.Controls[i].GetType() == typeof(UC_Phong))
                    {
                        UC_Phong temp = (UC_Phong)panelPhong.Controls[i];
                        if (IsSelected(temp, p))
                        {
                            if (temp._Phong.TrangThai1.MaTTP == ((int)TrangThaiPhong.dangTrong).ToString() || temp==phong)
                                temp.Mo();
                        }
                        else
                        {
                            temp.Sang();
                        }
                    }
                }
                phong.Location = p;

            }
            catch { }
        }
      
      
        public void Phong_Up(UC_Phong phong)
        {
            try
            {
                for (int i = 0; i < panelPhong.Controls.Count; i++)
                {
                    if (panelPhong.Controls[i].GetType() == typeof(UC_Phong))
                    {
                        Point c = System.Windows.Forms.Cursor.Position;
                        Point p = this.PointToClient(c);
                        p.Y = p.Y - panelThongTinSinhNhat.Height;
                        UC_Phong uc=(UC_Phong)panelPhong.Controls[i];
                        if (IsSelected(uc, p) && uc != phong && uc._Phong.TrangThai1.MaTTP == ((int)TrangThaiPhong.dangTrong).ToString())
                        {
                            uc._Phong.TrangThai1.SwapStates( uc._Phong.TrangThai1,  phong._Phong.TrangThai1);

                            uc._TrangThaiPhong.BackColor =Color.FromArgb(uc._Phong.TrangThai1.Mau);
                            phong._TrangThaiPhong.BackColor = Color.FromArgb(phong._Phong.TrangThai1.Mau);
                            phong._TrangThaiPhong.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
                            uc._TrangThaiPhong.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
                            XLDuLieu.ExecuteNonQuery(string.Format("update Phong set MaTTP={0} where MaPH='{1}'", phong._Phong.TrangThai1.MaTTP,phong._Phong.MaPH));
                            XLDuLieu.ExecuteNonQuery(string.Format("update Phong set MaTTP={0} where MaPH='{1}'", uc._Phong.TrangThai1.MaTTP, uc._Phong.MaPH));
                            break;
                        }
                    }
                }
            }
            catch { }
        }
        void tao_day(int n, int m)
        {
            mau = new List<int>();
            Random r = new Random();
            for (int i = 0; i < n; i++)
            {
                mau.Add(i);
            }
            for (int i = 0; i < m; i++)
            {
                int cs = r.Next(i, n);
                int temp = mau[i];
                mau[i] = mau[cs];
                mau[cs] = temp;
            }

        }
        private void HienTang(object sender)
        {

        }
        void ButtonStyle(FlatButton button)
        { 
            button.Font = new System.Drawing.Font("Times New Roman", 10F);
            button.Location = new System.Drawing.Point(3, 3);
        }
        Point CurrentKhu = new Point(0, 0);
        Point CurrentTang = new Point(120, 0);
        Point CurrentPhong = new Point(5, 180);
        Point menuPostion = new Point(0, 2);
        int magin = 5;
        int magin_phong = 10;
        int trangHienTai = -1;
        private void SoDoPhong_Load(object sender, EventArgs e)
        {
            VeSoDo(1);
        }
       
       
        void fpl_Click(object o,EventArgs e)
        {
            Button flat = (Button)o;
            int i = int.Parse(flat.Tag.ToString());
            trangHienTai = i;
            veLai(i,tbLyle.Value);
        }
        void veLai(int i,int tyle)
        {

            panelPhong.Controls.Clear();
            CurrentKhu.Y = magin;
            CurrentKhu.X = magin;
            CurrentTang.Y = magin;
            if (i == -1)
            {
             
                VeSoDo(1);
            }
            else
                LoadTang(i,tyle);
        }
        private void VeSoDo(int tile)
        {
            try
            {
                khu = XLDuLieu.LoadDataTable("select * from KhuVuc");
                CurrentKhu.Y = magin;
                CurrentKhu.X = magin;
                CurrentTang.Y = magin;
               
             
                for (int i = 0; i < khu.Rows.Count; i++)
                {
                    string tilte = khu.Rows[i]["DienGiai"].ToString();
                    LoadTang(i, tile);
                }
                thongTinPhong = new F_ThongTinPhong();
                thongTinPhong.FormClosed += new FormClosedEventHandler(FormClosedEventHandler);
            }
            catch { }
        }
        void fbKhu_MouseHover(object sender, EventArgs e)
        {
            Button fbKhu = (Button)sender;
            fbKhu.FlatAppearance.BorderSize = 1;
            fbKhu.FlatAppearance.BorderColor =Color.Lime; 
        }
        void fbKhu_MouseLeave(object sender, EventArgs e)
        {
            Button fbKhu = (Button)sender;
            DefaultButtonStyles(fbKhu);
        }
        private void LoadTang(int i,int tyle)
        {
            string trangThaiPhong;

            Button fbKhu = new Button();
            DefaultButtonStyles(fbKhu);
            fbKhu.Click+=new EventHandler(fpl_Click);
            fbKhu.MouseHover += new EventHandler(fbKhu_MouseHover);
            fbKhu.MouseLeave += new EventHandler(fbKhu_MouseLeave);
            menuPostion.X += fbKhu.Width + magin;
            fbKhu.Tag = i;
            fbKhu.Text = khu.Rows[i]["DienGiai"].ToString();
            panelPhong.Controls.Add(fbKhu);
            string maKhu = khu.Rows[i]["MaKV"].ToString();
            tang = XLDuLieu.LoadDataTable(string.Format("Select * from TANG where MaKV=N'{0}'", maKhu));
            for (int t = 0; t < tang.Rows.Count; t++)
            {

                FlatPanelLabel fbTang = new FlatPanelLabel();
                fbTang.Text.Text = tang.Rows[t]["TenTang"].ToString();
                int tangHeight = TANG_HEIGHT - magin;
                CurrentTang.X = KHU_WIDTH + 2 * magin;
                fbTang.SetBounds(CurrentTang.X, CurrentTang.Y, TANG_WIDTH, tangHeight);

                panelPhong.Controls.Add(fbTang);

                string maTang = tang.Rows[t]["ID_Tang"].ToString();
                phong = XLDuLieu.LoadDataTable("sp_LayThongTinSoDoPhong " + maTang);
                CurrentPhong.X = CurrentTang.X + TANG_WIDTH + magin;
                CurrentPhong.Y = CurrentTang.Y;
                PHONG_WIDHT = PHONG_HEIGHT = (panelPhong.Width - 300 - magin * loop_x * tyle) / loop_x;

                for (int k = 0; k < phong.Rows.Count; k++)
                {
                    DataRow r = phong.Rows[k];
                    UC_Phong uc_Phong = new UC_Phong();

                    

                    #region thông tin phòng
                    Class.Phong objPhong = new Class.Phong(r["ID_Tang"].ToString()
                                                , r["TenTang"].ToString()
                                                , r["MaTTP"].ToString()
                                                , r["TrangThai"].ToString()
                                                , (int)r["Mau"]
                                                , r["MaPH"].ToString()
                                                , r["MaGia"].ToString()
                                                , r["DienGiaiGiaPhong"].ToString()
                                                , r["TuNgay"].ToString()
                                                , r["DenNgay"].ToString()
                                                , (int)r["Gia1Khach"]
                                                , (int)r["Gia2Khach"]
                                                , (int)r["Gia3Khach"]
                                                , (int)r["Gia4Khach"]
                                                , (int)r["GiaThemNGLon"]
                                                , (int)r["GiaThemTreEm"]
                                                , r["MaLP"].ToString()
                                                , r["LoaiPH"].ToString()
                                                , r["MaKP"].ToString()
                                                , r["DienGiaiKieuPhong"].ToString()
                                                , r["MaHangPhong"].ToString()
                                           );
                    #endregion

                    uc_Phong._Phong = objPhong;
                    uc_Phong._TieuDe.Text = objPhong.MaPH;
                    trangThaiPhong = objPhong.TrangThai1.MaTTP;

                    uc_Phong.Location = new Point(CurrentPhong.X, CurrentPhong.Y);
                    CurrentPhong.X += PHONG_WIDHT + magin_phong;

                    uc_Phong.di = new UC_Phong.leave(Phong_Leave);
                    uc_Phong.hover = new UC_Phong.Hover(Phong_Hover);
                    uc_Phong.Phong_Up = new UC_Phong.PhongUp(Phong_Up);
                    uc_Phong.Phong_Move = new UC_Phong.DuyChuyen(Phong_Move);
                    uc_Phong._TrangThaiPhong.BackColor = Color.FromArgb(objPhong.TrangThai1.Mau);
                    uc_Phong.Width = PHONG_WIDHT;
                    uc_Phong.Height = tangHeight;
                    //if (trangThaiPhong == ((int)TrangThaiPhong.coKhach).ToString())
                    //{
                    //    uc_Phong._State.BackColor = Color.FromArgb(-5000);
                    //}
                    //else
                    //{
                    //    uc_Phong._State.BackColor = Color.FromArgb(-10000);
                    //}
                    panelPhong.Controls.Add(uc_Phong);
                }
                CurrentTang.Y += TANG_HEIGHT;
            }
            fbKhu.SetBounds(CurrentKhu.X, CurrentKhu.Y, KHU_WIDTH, TANG_HEIGHT * tang.Rows.Count - magin);
            CurrentKhu.Y = CurrentTang.Y = CurrentTang.Y + magin_phong;

        }

        private static void DefaultButtonStyles(Button fbKhu)
        {
            fbKhu.FlatAppearance.BorderSize = 0;
            fbKhu.FlatStyle = FlatStyle.Flat;
            fbKhu.BackColor = SystemColors.GradientActiveCaption;
        }
        private void bnTrangThai_Click(object sender, EventArgs e)
        {
            F_TrangThaiPhongSetting trangThai = new F_TrangThaiPhongSetting();
            trangThai.ShowDialog();
            panelPhong.Controls.Clear();
            VeSoDo(1);
        }
        private void pnPhong_MouseWheel(object sender, MouseEventArgs e)
        {
           
            if (e.Delta < 0)
            {
                if (tbLyle.Value < tbLyle.Properties.Maximum)
                    tbLyle.Value += 3;
            }
            else
                if (tbLyle.Value > tbLyle.Properties.Minimum)
                    tbLyle.Value -=3;
        }

        private void tbLyle_EditValueChanged(object sender, EventArgs e)
        {
            menuPostion = new Point(0, 2);
           
            if (trangHienTai == -1)
            {
                SizeF f = new SizeF(10, 10);
            }
            else
            {
                veLai(trangHienTai, -tbLyle.Value);
            }
        }

        private void FormClosedEventHandler(object sender, FormClosedEventArgs e)
        {
            
            this.ParentForm.Hide();

        }

    }

}
